{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "ee3a16c5-da70-4d74-a087-ef9b5e0801f7",
"metadata": {
"editable": true,
"nbsphinx": "hidden",
"slideshow": {
"slide_type": ""
},
"tags": []
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"%config InlineBackend.figure_format = 'svg'\n",
"\n",
"import warnings \n",
"warnings.filterwarnings(\"ignore\") #ignore some matplotlib warnings\n",
"\n",
"import numpy as np\n",
"\n",
"from mpi4py import MPI\n",
"\n",
"from triqs.plot.mpl_interface import plt, oplot, oplotr, oploti\n",
"plt.rcParams[\"figure.figsize\"] = (6,4) # set default size for all figures"
]
},
{
"cell_type": "markdown",
"id": "2105a30a-00d6-4a01-894e-31446ea063db",
"metadata": {},
"source": [
"# Semi-infinite chain\n",
"\n",
"This tutorial shows how to setup a minimal impurity model, that of a single fermionic state, and solve it using `triqs_xca`. As the first example we pick the analytically solvable case of a semi-infinte chain. (By having the exact solution we will be able to see how the bold hybridization expansion converges as a function of expansion order.)\n",
"\n",
"In the semi-infinite chain with nearest neighbour hopping $t = 1$ the site with only one neighbour has a single particle Green's function with semi-circular density of states (DOS). Thus the chain can be reformulated as an impurity problem of a single site (with zero energy) coupled to a hybridization function with semi-cicular DOS."
]
},
{
"cell_type": "markdown",
"id": "8c39feb5-50e7-4945-8c6f-69b3ccb6cc0b",
"metadata": {},
"source": [
"## Initialization\n",
"\n",
"First we spawn a solver instance and setting up a few general system parameters. The local Hamiltonian of the single-site is in this case trivial with a single state at zero energy $H = 0 \\cdot c^\\dagger c = 0 \\cdot \\hat{n}$."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "3bea90ac-4e06-4dc8-965a-2e1d013de004",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Starting run with 1 MPI rank(s) at : 2026-05-19 12:36:51.952515\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"____ ____________ _____\n",
"\\ \\/ /\\_ ___ \\ / _ \\\n",
" \\ / / \\ \\/ / /_\\ \\\n",
" / \\ \\ \\____/ | \\\n",
"/___/\\ \\ \\______ /\\____|__ /\n",
" \\_/ \\/ \\/ [github.com/TRIQS/xca]\n",
"\n",
"beta = 5.0, w_max = 2.0, eps = 1e-08, N_DLR = 13\n",
"AtomDiagReal: dim 2 with 2 subspaces dims [1] freq [2] E min/max +0.00E+00/+0.00E+00\n"
]
}
],
"source": [
"from triqs.operators import n\n",
"\n",
"from triqs_xca import BlockSparseSolver as Solver\n",
"\n",
"S = Solver(\n",
" H_loc=0.0 * n('0',0),\n",
" beta=5.0, # Inverse temperature\n",
" gf_struct=[('0', 1)], # Green's function structure 1st index: name, 2nd index: dimension of subspace\n",
" eps=1e-8, # Accuracy of Discrete Lehmann Representation (DLR) used for imaginary time response functions\n",
" w_max=2.0, # DLR frequency cut-off (the spectrum of the model must be in the range [-w_max, +w_max]\n",
" )"
]
},
{
"cell_type": "markdown",
"id": "da5e67dc-b514-4010-9cd8-9d580cd74c60",
"metadata": {},
"source": [
"## Hybridization function\n",
"\n",
"In the solver instance the hybridization function is located at `S.Delta_tau` and we set it to have the semi-circular shape of the semi-infinite chain."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "11d8861b-a877-4d03-8092-736728b2f244",
"metadata": {},
"outputs": [],
"source": [
"from triqs.gfs import make_gf_dlr_imtime, make_gf_dlr_imfreq, SemiCircular\n",
"\n",
"Delta_w = make_gf_dlr_imfreq(S.Delta_tau['0'])\n",
"Delta_w << SemiCircular(2.0)\n",
"S.Delta_tau['0'] = make_gf_dlr_imtime(Delta_w)"
]
},
{
"cell_type": "markdown",
"id": "593304cd-ed71-44b9-973b-ba7e15e9a7b9",
"metadata": {},
"source": [
"## Run solver\n",
"\n",
"Finally we run the solver for a few different expansion orders."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "06ca48e3-2414-4e7b-86e5-037b77796c90",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hybridization: using DLR expansion with N_poles = 13\n",
"iter = 1, diff_G = 1.80E-01, Z-1 = +1.78E-10, eta = 4.12E-01\n",
"iter = 2, diff_G = 7.66E-02, Z-1 = +1.89E-09, eta = 5.02E-01\n",
"iter = 3, diff_G = 1.66E-02, Z-1 = +4.52E-10, eta = 5.17E-01\n",
"iter = 4, diff_G = 2.09E-03, Z-1 = +5.23E-11, eta = 5.19E-01\n",
"iter = 5, diff_G = 1.59E-04, Z-1 = +3.64E-12, eta = 5.19E-01\n",
"iter = 6, diff_G = 8.13E-06, Z-1 = +1.72E-13, eta = 5.19E-01\n",
"Converged after 6 iterations with diff_G = 8.13E-06 < tol = 1.00E-04\n",
"\n",
"Timing: incl. excl.\n",
"----------------------------------------------------\n",
"Adapol hybridization fit: 0.000 0.000 1.1% |\n",
"AtomDiag Init: 0.000 0.000 0.5% |\n",
"DiagramEvaluator Init: 0.002 0.002 14.9% |-----|\n",
"Dyson: 0.002 0.000 2.4% ||\n",
" Setup: 0.001 0.001 8.2% |--|\n",
" Solve: 0.001 0.001 7.5% |--|\n",
"Sigma: 0.001 0.000 1.0% |\n",
" Order 1: 0.001 0.001 9.9% |---|\n",
"Single-particle Gf: 0.000 0.000 0.1% |\n",
" Order 1: 0.000 0.000 0.7% |\n",
"Other: 0.007 0.007 53.9% |---------------------|\n",
"----------------------------------------------------\n",
"Total: 0.014 100.0%\n",
"\n",
"AAA: Error 1.87E-01 using 1 support and 11 fitting points (step 1/12)\n",
"AAA: Error 6.48E-04 using 2 support and 9 fitting points (step 2/12)\n",
"AAA: Error 2.66E-07 using 3 support and 8 fitting points (step 3/12)\n",
"AAA: Converged after 3 steps with error 2.66E-07.\n",
"TDC: Error 6.33E-07 for 3 AAA steps (Error 6.48E-04 no opt) c.f. tol 1.00E-05.\n",
"AAA: Error 1.87E-01 using 1 support and 11 fitting points (step 1/1)\n",
"TDC: Error 1.91E-01 for 1 AAA steps (Error 7.34E-01 no opt) c.f. tol 1.00E-05.\n",
"AAA: Error 1.87E-01 using 1 support and 11 fitting points (step 1/2)\n",
"AAA: Error 6.48E-04 using 2 support and 9 fitting points (step 2/2)\n",
"TDC: Error 1.15E-03 for 2 AAA steps (Error 1.87E-01 no opt) c.f. tol 1.00E-05.\n",
"TDC: Compression finished with 3 AAA steps and error 6.33E-07.\n",
"Adapol: Fit error = 6.33E-07 < tol_adapol = 1.00E-05, N_poles = 5\n",
"iter = 1, diff_G = 8.97E-06, Z-1 = +7.13E-14, eta = 5.19E-01\n",
"Converged after 1 iterations with diff_G = 8.97E-06 < tol = 1.00E-04\n",
"\n",
"Timing: incl. excl.\n",
"----------------------------------------------------\n",
"Adapol hybridization fit: 0.009 0.009 9.3% |---|\n",
"AtomDiag Init: 0.000 0.000 0.1% |\n",
"DiagramEvaluator Init: 0.002 0.002 2.1% ||\n",
"Dyson: 0.003 0.000 0.4% |\n",
" Setup: 0.001 0.001 1.1% |\n",
" Solve: 0.001 0.001 1.2% |\n",
"Sigma: 0.002 0.000 0.2% |\n",
" Order 1: 0.002 0.002 1.6% ||\n",
" Order 2: 0.000 0.000 0.2% |\n",
"Single-particle Gf: 0.001 0.000 0.0% |\n",
" Order 1: 0.000 0.000 0.2% |\n",
" Order 2: 0.001 0.001 0.5% |\n",
"Other: 0.083 0.083 83.1% |--------------------------------|\n",
"----------------------------------------------------\n",
"Total: 0.100 100.0%\n",
"\n",
"AAA: Error 1.87E-01 using 1 support and 11 fitting points (step 1/12)\n",
"AAA: Error 6.48E-04 using 2 support and 9 fitting points (step 2/12)\n",
"AAA: Error 2.66E-07 using 3 support and 8 fitting points (step 3/12)\n",
"AAA: Converged after 3 steps with error 2.66E-07.\n",
"TDC: Error 6.33E-07 for 3 AAA steps (Error 6.48E-04 no opt) c.f. tol 1.00E-05.\n",
"AAA: Error 1.87E-01 using 1 support and 11 fitting points (step 1/1)\n",
"TDC: Error 1.91E-01 for 1 AAA steps (Error 7.34E-01 no opt) c.f. tol 1.00E-05.\n",
"AAA: Error 1.87E-01 using 1 support and 11 fitting points (step 1/2)\n",
"AAA: Error 6.48E-04 using 2 support and 9 fitting points (step 2/2)\n",
"TDC: Error 1.15E-03 for 2 AAA steps (Error 1.87E-01 no opt) c.f. tol 1.00E-05.\n",
"TDC: Compression finished with 3 AAA steps and error 6.33E-07.\n",
"Adapol: Fit error = 6.33E-07 < tol_adapol = 1.00E-05, N_poles = 5\n",
"iter = 1, diff_G = 1.13E-02, Z-1 = -3.56E-10, eta = 5.08E-01\n",
"iter = 2, diff_G = 1.81E-03, Z-1 = -4.16E-11, eta = 5.07E-01\n",
"iter = 3, diff_G = 1.50E-04, Z-1 = -3.00E-12, eta = 5.06E-01\n",
"iter = 4, diff_G = 8.04E-06, Z-1 = -1.46E-13, eta = 5.06E-01\n",
"Converged after 4 iterations with diff_G = 8.04E-06 < tol = 1.00E-04\n",
"\n",
"Timing: incl. excl.\n",
"----------------------------------------------------\n",
"Adapol hybridization fit: 0.012 0.012 7.1% |--|\n",
"AtomDiag Init: 0.000 0.000 0.0% |\n",
"DiagramEvaluator Init: 0.002 0.002 1.2% |\n",
"Dyson: 0.005 0.001 0.5% |\n",
" Setup: 0.001 0.001 0.7% |\n",
" Solve: 0.003 0.003 1.6% ||\n",
"Sigma: 0.033 0.000 0.2% |\n",
" Order 1: 0.003 0.003 1.6% ||\n",
" Order 2: 0.002 0.002 0.9% |\n",
" Order 3: 0.029 0.029 16.8% |------|\n",
"Single-particle Gf: 0.015 0.000 0.0% |\n",
" Order 1: 0.000 0.000 0.2% |\n",
" Order 2: 0.001 0.001 0.6% |\n",
" Order 3: 0.013 0.013 7.8% |--|\n",
"Other: 0.104 0.104 60.7% |-----------------------|\n",
"----------------------------------------------------\n",
"Total: 0.172 100.0%\n",
"\n",
"AAA: Error 1.87E-01 using 1 support and 11 fitting points (step 1/12)\n",
"AAA: Error 6.48E-04 using 2 support and 9 fitting points (step 2/12)\n",
"AAA: Error 2.66E-07 using 3 support and 8 fitting points (step 3/12)\n",
"AAA: Converged after 3 steps with error 2.66E-07.\n",
"TDC: Error 6.33E-07 for 3 AAA steps (Error 6.48E-04 no opt) c.f. tol 1.00E-05.\n",
"AAA: Error 1.87E-01 using 1 support and 11 fitting points (step 1/1)\n",
"TDC: Error 1.91E-01 for 1 AAA steps (Error 7.34E-01 no opt) c.f. tol 1.00E-05.\n",
"AAA: Error 1.87E-01 using 1 support and 11 fitting points (step 1/2)\n",
"AAA: Error 6.48E-04 using 2 support and 9 fitting points (step 2/2)\n",
"TDC: Error 1.15E-03 for 2 AAA steps (Error 1.87E-01 no opt) c.f. tol 1.00E-05.\n",
"TDC: Compression finished with 3 AAA steps and error 6.33E-07.\n",
"Adapol: Fit error = 6.33E-07 < tol_adapol = 1.00E-05, N_poles = 5\n",
"iter = 1, diff_G = 2.50E-03, Z-1 = +6.10E-11, eta = 5.08E-01\n",
"iter = 2, diff_G = 2.46E-04, Z-1 = +5.21E-12, eta = 5.09E-01\n",
"iter = 3, diff_G = 1.45E-05, Z-1 = +2.88E-13, eta = 5.09E-01\n",
"Converged after 3 iterations with diff_G = 1.45E-05 < tol = 1.00E-04\n",
"\n",
"Timing: incl. excl.\n",
"----------------------------------------------------\n",
"Adapol hybridization fit: 0.015 0.015 1.2% |\n",
"AtomDiag Init: 0.000 0.000 0.0% |\n",
"DiagramEvaluator Init: 0.002 0.002 0.2% |\n",
"Dyson: 0.006 0.001 0.1% |\n",
" Setup: 0.001 0.001 0.1% |\n",
" Solve: 0.003 0.003 0.3% |\n",
"Sigma: 0.431 0.000 0.0% |\n",
" Order 1: 0.005 0.005 0.4% |\n",
" Order 2: 0.003 0.003 0.2% |\n",
" Order 3: 0.067 0.067 5.2% |-|\n",
" Order 4: 0.356 0.356 27.5% |----------|\n",
"Single-particle Gf: 0.716 0.000 0.0% |\n",
" Order 1: 0.000 0.000 0.0% |\n",
" Order 2: 0.002 0.002 0.1% |\n",
" Order 3: 0.027 0.027 2.1% ||\n",
" Order 4: 0.687 0.687 53.1% |--------------------|\n",
"Other: 0.124 0.124 9.6% |---|\n",
"----------------------------------------------------\n",
"Total: 1.294 100.0%\n",
"\n"
]
}
],
"source": [
"from h5 import HDFArchive\n",
"\n",
"max_order = 4\n",
"for order in range(1, max_order+1):\n",
" S.solve(max_order=order, maxiter=20)\n",
" with HDFArchive(f'data_chain_order_{order}.h5', 'w') as A: A['S'] = S"
]
},
{
"cell_type": "markdown",
"id": "aa2f25aa-9993-47ed-8612-58d292a54357",
"metadata": {},
"source": [
"## Visualization\n",
"\n",
"To see the convergence with expansion order we plot the resulting single-particle Green's function."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "ce78e650-b9bb-4b65-8209-55626b9cb583",
"metadata": {},
"outputs": [],
"source": [
"from triqs.plot.mpl_interface import oplot, oplotr, oploti, plt\n",
"\n",
"def plot_dlr_imtime(g_tau, label, n_tau=400, marker='x', linestyle='-', color=None):\n",
"\n",
" from triqs.gfs import make_gf_imtime\n",
"\n",
" g_tau_fine = make_gf_imtime(g_tau, n_tau=n_tau)\n",
"\n",
" if color is None:\n",
" color = plt.plot([], [], linestyle+marker, label=label)[0].get_color()\n",
" label = None\n",
"\n",
" oplotr(g_tau, marker=marker, label=None, color=color)\n",
" oplotr(g_tau_fine, label=label, color=color, linestyle=linestyle)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "b2b58b70-083c-4c07-8d7d-5a6da7e261cf",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"--> Loading: data_chain_order_1.h5\n",
"--> Loading: data_chain_order_2.h5\n",
"--> Loading: data_chain_order_3.h5\n",
"--> Loading: data_chain_order_4.h5\n"
]
}
],
"source": [
"import glob\n",
"\n",
"filenames = np.sort(glob.glob('data_chain_order_*.h5'))\n",
"\n",
"results = []\n",
"for filename in filenames:\n",
" print(f'--> Loading: {filename}')\n",
" with HDFArchive(filename, 'r') as A:\n",
" results.append(A['S'])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "e3f1be9b-4b87-461d-aeed-346948beb746",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"for r in results:\n",
" plot_dlr_imtime(r.G_tau['0'], label=f'Order {r.max_order}', \n",
" linestyle='-' if r.max_order != 2 else ':')\n",
"\n",
"delta_tau = results[0].Delta_tau['0']\n",
"plot_dlr_imtime(delta_tau, label='Exact', marker='', linestyle='-.', color='black')\n",
"\n",
"plt.ylabel(r'$G(\\tau)$'); plt.xlabel(r'$\\tau$')\n",
"plt.legend(loc='best'); plt.grid(True); plt.ylim(top=0);"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "1f93ea29-7dd2-4c30-9f3f-b12192a6d615",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"for r in results:\n",
" diff_tau = delta_tau - r.G_tau['0']\n",
" diff_tau.data[:] = np.abs(diff_tau.data)\n",
" plot_dlr_imtime(diff_tau, label=f'Order {r.max_order}',\n",
" linestyle='-' if r.max_order != 2 else ':')\n",
"\n",
"plt.semilogy([], [])\n",
"plt.legend(loc='best')\n",
"\n",
"plt.ylabel(r'max$|G(\\tau) - G_{exact}(\\tau)|$')\n",
"plt.xlabel(r'$\\tau$')\n",
"plt.grid(True)"
]
},
{
"cell_type": "markdown",
"id": "cdf81137-b2a1-4d9b-a7c6-3a8cee56b7a3",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"source": [
"The convergence rate with expansion order is fast and the error at order 4 is about $10^{-3}$."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}